package com.queclink.ycm.entity.data;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.queclink.ycm.common.utils.HexUtils;
import com.queclink.ycm.entity.data.Data;

import java.text.DecimalFormat;

/**
 * @author jim.li
 * @email jim.li@queclink.com
 * @date 2021-12-27 11:03:00
 */
public class Data11 extends Data {


    private Integer InfoLength;

    private String Info;

    public String getDataID() {

        return "0B";
    }

    @JSONField(
            name = "InfoLength"
    )
    public String getInfoLength(JSONObject jsonObject) {
        Integer infoLength = jsonObject.getInteger("InfoLength");
        return HexUtils.intToHex(infoLength, 2);
    }

    public void setInfoLength(Integer infoLength) {
        InfoLength = infoLength;
    }


    @JSONField(
            name = "Info"
    )
    public String getInfo(JSONObject jsonObject) {
        String Info = jsonObject.getString("Info");
        // 创建 StringBuilder 来存储转换后的结果
        StringBuffer hexString = new StringBuffer();

        // 遍历字符串的每个字符，将其转为对应的十六进制值
        for (char c : Info.toCharArray()) {
            hexString.append(String.format("%02X", (int) c));  // 格式化为两位大写十六进制
        }

        // 添加 00 字节
        hexString.append("00");
        return hexString.toString();
    }

    public void setInfo(String info) {
        Info = info;
    }

    @Override
    public String combine(JSONObject jsonObject) throws Exception {
        StringBuffer sb = new StringBuffer();
        sb.append("0000");
        sb.append(this.getInfoLength(jsonObject));
        sb.append(this.getInfo(jsonObject));
        int len = sb.length();
        len = len / 2;
        if (len > 127) {
            sb.insert(0, HexUtils.intToHex(len, 4));
        } else {
            sb.insert(0, HexUtils.intToHex(len, 2));
        }
        sb.insert(0, this.getDataID());
        return sb.toString();
    }
}
